****************
*** HNR Police Perceptions
*** Dotan Haim, Matthew Nanes & Nico Ravanilla
*** July 2024
*** Replication code
***************

/* Data accessed in this replication file:
POLICE_wide_bootstrapped_and_weighted.dta
*/

*** Created in STATA/SE version 18.0

clear all
set more off

*** Install commands
*ssc install outreg2,replace

*** Change to appropriate working directory (e.g.)
cd "~/Dropbox/DHNR Philippine Police/3. EGAP (POP - One Sorsogon Reloaded)/3.14 - Replication Files/Effects on PNP/HRS_APSR_Replication/3_Tables/0_raw"

*** Create log file
capture log close
log using bootstrapped_logfile.log, replace


*****************************************************
* BOOTSTRAPPED RESULTS: Replicate Manuscript Tables *
*****************************************************

set seed 123456
import delimited "~/Dropbox/DHNR Philippine Police/3. EGAP (POP - One Sorsogon Reloaded)/3.14 - Replication Files/Effects on PNP/HRS_APSR_Replication/2_Data/POLICE_wide.csv",clear

* Replicate Table A.5: Main Treatment Effects
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx {
bootstrap: areg `i'_e  z_pop_assign `i'_m miss_bline rank_m_spo, vce(cl station_m) absorb(station_m)
estimates store `i'_p
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_p] using  "TabA15_MainResults_bootstrapped.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_p] using  "TabA15_MainResults_bootstrapped.xls", append  tex dec(2)
}

bootstrap: areg `i'_e  z_pop_assign miss_bline rank_m_spo, vce(cl station_m) absorb(station_m)
estimates store `i'_c
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_c] using  "TabA15_MainResults_bootstrapped.xls", append  tex dec(2)
}
else {
outreg2 [`i'_c] using  "TabA15_MainResults_bootstrapped.xls", append  tex dec(2)
}
}

restore

*Replicate Table A.6: Heterogeneous Treatment Effects - Embeddedness
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx {
bootstrap: areg `i'_e  i.z_pop_assign##i.lives_in_sorsogon `i'_m miss_bline rank_m_spo, vce(cl station_m) absorb(station_m)
estimates store `i'_he
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_he] using  "TabA16_HetEffects_Embed_bootstrapped.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_he] using  "TabA16_HetEffects_Embed_bootstrapped.xls", append  tex dec(2)
}
}

restore

*Replicate Table A.7: Heterogeneous Treatment Effects - Rebel Presence (Trust Outcomes)
preserve
foreach i in trust_idx trust_safe trust_intent trust_info npa_sympathy {
bootstrap: reg `i'_e  i.z_pop_assign##c.npa_2015_presence `i'_m miss_bline rank_m_spo, vce(cl station_m)
estimates store `i'_hn
if "`i'" == "trust_idx" {
outreg2 [`i'_hn] using  "TabA17_HetEffects_NPAsub_bootstrapped.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_hn] using  "TabA17_HetEffects_NPAsub_bootstrapped.xls", append  tex dec(2)
}
}

restore

*Replicate Table A.8: Heterogeneous Treatment Effects - Rebel Presence
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx trust_idx_alt {
bootstrap: reg `i'_e  i.z_pop_assign##c.npa_2015_presence `i'_m miss_bline rank_m_spo, vce(cl station_m)
estimates store `i'_hn
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_hn] using  "TabA18_HetEffects_NPA_bootstrapped.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_hn] using  "TabA18_HetEffects_NPA_bootstrapped.xls", append tex dec(2)
}
}

restore


*****************************************************
* WEIGHTED REGRESSION: Replicate Manuscript Tables *
*****************************************************

*Count of POP-treated barangays by police station_m
*Barcelona	3
*Bulan	13
*Bulusan	5
*Casiguran	5
*Castilla	8
*Donsol	9
*Gubat	10
*Irosin	4
*Juban	5
*Magallanes	4
*Matnog	4
*Pilar	9
*Prieto Diaz	4
*Santa Magdalena	2
*Sorsogon	14

gen count_pop_bgys_per_station_m=.
replace count_pop_bgys_per_station_m =3 if station_m=="barcelona"
replace count_pop_bgys_per_station_m =13 if station_m=="bulan"
replace count_pop_bgys_per_station_m =5 if station_m=="bulusan"
replace count_pop_bgys_per_station_m =5 if station_m=="casiguran"
replace count_pop_bgys_per_station_m =8 if station_m=="castilla"
replace count_pop_bgys_per_station_m =9 if station_m=="donsol"
replace count_pop_bgys_per_station_m =10 if station_m=="gubat"
replace count_pop_bgys_per_station_m =4 if station_m=="irosin"
replace count_pop_bgys_per_station_m =5 if station_m=="juban"
replace count_pop_bgys_per_station_m =4 if station_m=="magallanes"
replace count_pop_bgys_per_station_m =4 if station_m=="matnog"
replace count_pop_bgys_per_station_m =9 if station_m=="pilar"
replace count_pop_bgys_per_station_m =4 if station_m=="prietodias"
replace count_pop_bgys_per_station_m =2 if station_m=="santamagdalena"
replace count_pop_bgys_per_station_m =14 if station_m=="sorsogon1"
replace count_pop_bgys_per_station_m =14 if station_m=="sorsogon2"
replace count_pop_bgys_per_station_m =14 if station_m=="sorsogonppo"

gen station_m_dup_for_weighted_reg=station_m
replace station_m_dup_for_weighted_reg="sorsogon" if station_m=="sorsogon1"
replace station_m_dup_for_weighted_reg="sorsogon" if station_m=="sorsogon2"
replace station_m_dup_for_weighted_reg="sorsogon" if station_m=="sorsogonppo"

gen weighted_reg_denominator=.
replace weighted_reg_denominator=20 if station_m=="barcelona" & rank_m_spo==0
replace weighted_reg_denominator=35 if station_m=="bulan" & rank_m_spo==0
replace weighted_reg_denominator=16 if station_m=="bulusan" & rank_m_spo==0
replace weighted_reg_denominator=21 if station_m=="casiguran" & rank_m_spo==0
replace weighted_reg_denominator=23 if station_m=="castilla" & rank_m_spo==0
replace weighted_reg_denominator=20 if station_m=="donsol" & rank_m_spo==0
replace weighted_reg_denominator=23 if station_m=="gubat" & rank_m_spo==0
replace weighted_reg_denominator=25 if station_m=="irosin" & rank_m_spo==0
replace weighted_reg_denominator=18 if station_m=="juban" & rank_m_spo==0
replace weighted_reg_denominator=20 if station_m=="magallanes" & rank_m_spo==0
replace weighted_reg_denominator=21 if station_m=="matnog" & rank_m_spo==0
replace weighted_reg_denominator=27 if station_m=="pilar" & rank_m_spo==0
replace weighted_reg_denominator=11 if station_m=="prietodias" & rank_m_spo==0
replace weighted_reg_denominator=22 if station_m=="santamagdalena" & rank_m_spo==0
replace weighted_reg_denominator=245 if station_m=="sorsogon1" & rank_m_spo==0
replace weighted_reg_denominator=245 if station_m=="sorsogon2" & rank_m_spo==0
replace weighted_reg_denominator=245 if station_m=="sorsogonppo" & rank_m_spo==0

replace weighted_reg_denominator=4 if station_m=="barcelona" & rank_m_spo==1
replace weighted_reg_denominator=15 if station_m=="bulan" & rank_m_spo==1
replace weighted_reg_denominator=8 if station_m=="bulusan" & rank_m_spo==1
replace weighted_reg_denominator=4 if station_m=="casiguran" & rank_m_spo==1
replace weighted_reg_denominator=7 if station_m=="castilla" & rank_m_spo==1
replace weighted_reg_denominator=9 if station_m=="donsol" & rank_m_spo==1
replace weighted_reg_denominator=8 if station_m=="gubat" & rank_m_spo==1
replace weighted_reg_denominator=9 if station_m=="irosin" & rank_m_spo==1
replace weighted_reg_denominator=8 if station_m=="juban" & rank_m_spo==1
replace weighted_reg_denominator=6 if station_m=="magallanes" & rank_m_spo==1
replace weighted_reg_denominator=5 if station_m=="matnog" & rank_m_spo==1
replace weighted_reg_denominator=5 if station_m=="pilar" & rank_m_spo==1
replace weighted_reg_denominator=9 if station_m=="prietodias" & rank_m_spo==1
replace weighted_reg_denominator=4 if station_m=="santamagdalena" & rank_m_spo==1
replace weighted_reg_denominator=50 if station_m=="sorsogon1" & rank_m_spo==1
replace weighted_reg_denominator=50 if station_m=="sorsogon2" & rank_m_spo==1
replace weighted_reg_denominator=50 if station_m=="sorsogonppo" & rank_m_spo==1

*Simple weight; assumes weight=0 if station_m=="."
gen simple_weight=count_pop_bgys_per_station_m/weighted_reg_denominator
replace simple_weight=1 if simple_weight>1

*Adjusted weight; assumes weight=0 if station_m=="."; 0.19 likelihood that original assigned officer drops out; "control" officer is included in replacement
gen adjusted_weight=count_pop_bgys_per_station_m/weighted_reg_denominator
replace adjusted_weight=1 if adjusted_weight>1
replace adjusted_weight=(count_pop_bgys_per_station_m/weighted_reg_denominator)+(0.19*(1/(weighted_reg_denominator-count_pop_bgys_per_station_m))) if z_pop_assign==0 & station_m!=""

*Replicate Table A.5: Main Treatment Effects
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx {
areg `i'_e  z_pop_assign `i'_m miss_bline rank_m_spo [pweight=simple_weight], vce(cl station_m) absorb(station_m)
estimates store `i'_pw
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_pw] using  "TabA19_MainResults_weighted.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_pw] using  "TabA19_MainResults_weighted.xls", append  tex dec(2)
}
areg `i'_e  z_pop_assign miss_bline rank_m_spo [pweight=simple_weight], vce(cl station_m) absorb(station_m)
estimates store `i'_cw
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_cw] using  "TabA19_MainResults_weighted.xls", append  tex dec(2)
}
else {
outreg2 [`i'_cw] using  "TabA19_MainResults_weighted.xls", append  tex dec(2)
}
}

restore

*Replicate Table A.6: Heterogeneous Treatment Effects - Embeddedness
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx {
areg `i'_e  i.z_pop_assign##i.lives_in_sorsogon `i'_m miss_bline rank_m_spo [pweight=simple_weight], vce(cl station_m) absorb(station_m)
estimates store `i'_hw
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_hw] using  "TabA20_HetEffects_Embed_weighted.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_hw] using  "TabA20_HetEffects_Embed_weighted.xls", append  tex dec(2)
}
areg officer_attitude_idx_e  i.z_pop_assign##i.lives_in_sorsogon officer_attitude_idx_m miss_bline rank_m_spo [pweight=adjusted_weight], vce(cl station_m) absorb(station_m)
estimates store officer_attitude_idx_wa
outreg2 [officer_attitude_idx_wa] using  "TabA20_HetEffects_Embed_weighted.xls", append tex dec(2)
}

restore

*Replicate Table A.7: Heterogeneous Treatment Effects - Rebel Presence (Trust Outcomes)
preserve
foreach i in trust_idx trust_safe trust_intent trust_info npa_sympathy {
reg `i'_e  i.z_pop_assign##c.npa_2015_presence `i'_m miss_bline rank_m_spo [pweight=simple_weight], vce(cl station_m)
estimates store `i'_nw
if "`i'" == "trust_idx" {
outreg2 [`i'_nw] using  "TabA21_HetEffects_NPAsub_weighted.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_nw] using  "TabA21_HetEffects_NPAsub_weighted.xls", append  tex dec(2)
}
reg trust_idx_e i.z_pop_assign##c.npa_2015_presence trust_idx_m miss_bline rank_m_spo [pweight=adjusted_weight], vce(cl station_m)
estimates store trust_idx_wa
outreg2 [trust_idx_wa] using  "TabA21_HetEffects_NPAsub_weighted.xls", append tex dec(2)
}

restore

*Replicate Table A.8: Heterogeneous Treatment Effects - Rebel Presence
preserve
foreach i in top4match_pnplgu officer_attitude_idx trust_idx empathy_idx accountability_idx corrupt_idx trust_idx_alt {
reg `i'_e  i.z_pop_assign##c.npa_2015_presence `i'_m miss_bline rank_m_spo [pweight=simple_weight], vce(cl station_m)
estimates store `i'_ww
if "`i'" == "top4match_pnplgu" {
outreg2 [`i'_ww] using  "TabA22_HetEffects_NPA_weighted.xls", replace  tex dec(2)
}
else {
outreg2 [`i'_ww] using  "TabA22_HetEffects_NPA_weighted.xls", append tex dec(2)
}
}

restore

log close
